<template>
  <div>
    <div class="pageTopTitle">渠道跟进管理</div>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
        <el-form-item label="跟进方式" prop="method">
          <el-select v-model="queryParams.method" placeholder="请选择跟进方式">
            <el-option label="电话" value="1"></el-option>
            <el-option label="短信" value="2"></el-option>
            <el-option label="上门拜访" value="3"></el-option>
            <el-option label="邮件" value="4"></el-option>
            <el-option label="其他" value="5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="app-container mt20">

      <el-row :gutter="10" class="mb20">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="upList">
        <el-table-column label="跟进渠道" prop="clientId">
          <template slot-scope="scope">
            <span>{{ scope.row.channel.name || '未知渠道' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="跟进内容" prop="content"/>
        <el-table-column label="跟进方式" prop="method">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.method === '1'">电话</el-tag>
            <el-tag v-if="scope.row.method === '2'">短信</el-tag>
            <el-tag v-if="scope.row.method === '3'">上门拜访</el-tag>
            <el-tag v-if="scope.row.method === '4'">邮件</el-tag>
            <el-tag v-if="scope.row.method === '5'">其他</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="跟进图片" prop="images"/>
        <el-table-column label="跟进时间" prop="createAt" width="180"/>
        <el-table-column label="操作" class-name="small-padding fixed-width" width="120">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.row)"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改客户跟进记录对话框 -->
      <el-drawer
        :title="title"
        :visible.sync="open"
        size="35%"
        :wrapper-closable="false"
      >
        <div class="drawer-content">
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="跟进渠道" prop="channelId">
              <el-input placeholder="请选择渠道" disabled v-model="form.channelName" @keyup.enter.native="handleQuery">
                <el-button slot="append" style="background: #165DFF; color: #fff;" @click="chooseClient">请选择渠道
                </el-button>
              </el-input>
            </el-form-item>
            <el-form-item label="跟进方式" prop="method">
              <el-select v-model="form.method" placeholder="请选择跟进方式" style="width: 100%;">
                <el-option label="电话" value="1"></el-option>
                <el-option label="短信" value="2"></el-option>
                <el-option label="上门拜访" value="3"></el-option>
                <el-option label="邮件" value="4"></el-option>
                <el-option label="其他" value="5"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="跟进内容" prop="content">
              <el-input type="textarea" rows="4" v-model="form.content" placeholder="请输入跟进内容"/>
            </el-form-item>
            <el-form-item label="跟进图片" prop="images">
              <ImageUpload :limit="3" v-model="form.images"/>
            </el-form-item>
          </el-form>
        </div>
        <div class="drawer-footer">
          <el-button type="primary" style="width: 120px;" @click="submitForm" size="small">确 定</el-button>
          <el-button @click="cancel" style="width: 120px;" size="small">取 消</el-button>
        </div>
      </el-drawer>
      <el-dialog
        title="选择渠道"
        :visible.sync="channelDialogVisible"
        width="35%"
      >
        <div style="margin-bottom: 20px;">
          <el-input placeholder="请输入渠道姓名" style="width: 160px;margin-right: 10px"
                    v-model="channelQueryParams.name"
                    @keyup.enter.native="getChannelList"
          >
          </el-input>
          <el-button icon="el-icon-search" style="background: #165DFF; color: #fff;" @click="getChannelList">查找
          </el-button>
        </div>

        <el-table :data="channelList" style="width: 100%;" height="300px">
          <el-table-column label="渠道名称" prop="name"/>
          <el-table-column label="渠道号" prop="code"/>
          <el-table-column label="城市" prop="cityName"/>
          <el-table-column label="操作" width="80">
            <template slot-scope="scope">
              <el-button type="text" size="mini" @click="handleSelect(scope.row)">选择</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { listUp, getUp, delUp, addUp, updateUp } from '@/api/web/ChannelFollowUp'
import { listChannel } from '@/api/web/channel'
import { mapGetters } from 'vuex'

export default {
  name: 'Up',
  computed: {
    ...mapGetters([
      'userId',
    ])
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户跟进记录表格数据
      upList: [],

      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      channelDialogVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        channelId: null,
        userId: null,
        method: null
      },
      // 客户列表
      channelList: [],
      // 查询参数
      channelQueryParams: {
        pageNum: 1,
        pageSize: 10,
        businessUserId: null,
        name: null,
        phone: null
      },

      // 表单参数
      form: {},
      // 表单校验
      rules: {
        channelId: [
          { required: true, message: '跟进渠道不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '跟进不能为空', trigger: 'blur' }
        ],
        method: [
          { required: true, message: '跟进方式不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询客户跟进记录列表 */
    getList() {
      this.loading = true
      listUp(this.queryParams).then(response => {
        this.upList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },

    chooseClient() {
      this.channelDialogVisible = true
      this.channelQueryParams.businessUserId = this.userId
      this.channelQueryParams.pageNum = 1
      this.getChannelList()
    },

    handleSelect(row) {
      this.form.channelId = row.id
      this.form.channelName = row.name
      this.channelDialogVisible = false
    },

    // 表单重置
    reset() {
      this.form = {
        id: null,
        channelId: null,
        userId: null,
        content: null,
        method: null,
        images: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    getChannelList() {
      listChannel(this.channelQueryParams).then(response => {
        this.channelList = response.rows
      })
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加客户跟进记录'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getUp(id).then(response => {
        this.form = response.data
        if (this.form.channel) {
          this.form.channelName = this.form.channel.name
        }
        this.open = true
        this.title = '修改客户跟进记录'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUp(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addUp(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除客户跟进记录编号为"' + ids + '"的数据项？').then(function() {
        return delUp(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.drawer-content {
  padding: 0 30px 80px 20px;
}

.drawer-footer {
  position: absolute;
  bottom: 0;
  box-sizing: border-box;
  text-align: right;
  padding: 16px;
  width: 100%;
  border-top: 1px solid #eee;
  background-color: #fff;
}

::v-deep .el-form-item__label {
  font-weight: 500 !important;
}

::v-deep .el-form--label-top .el-form-item__label {
  padding: 0 0 4px 0 !important;
}
</style>

